package com.example.myapplication.rxjava;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;
import com.orhanobut.logger.Logger;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.Single;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * 类描述:https://zhuanlan.zhihu.com/p/196728913
 * 创建人:   tangchao$
 * 创建时间: 2021/8/28$ 9:38 PM$
 * 联系方式: 419704299@qq.com
 * 修改人:   tangchao$
 * 修改时间: 2021/8/28$ 9:38 PM$
 * 修改备注:  [说明本次修改内容]
 */
public class RxjavaErrorActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rxjavaerroractivity);
//        one1();
        t2();
//        t3();
    }

    private void t3() {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(@androidx.annotation.NonNull ObservableEmitter<Object> emitter) throws Exception {
                Logger.d("===subscribe: 1");
                emitter.onError(new Throwable("111"));
                Logger.d("===subscribe: 2");
                emitter.onError(new Throwable("2222"));
                Logger.d("===subscribe: 333");
                emitter.onComplete();

            }
        }).compose(new SchedulerTransformer<>()).subscribe(new Observer<Object>() {
            @Override
            public void onSubscribe(@androidx.annotation.NonNull Disposable d) {

            }

            @Override
            public void onNext(@androidx.annotation.NonNull Object o) {

            }

            @Override
            public void onError(@androidx.annotation.NonNull Throwable e) {
                Logger.d("===onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                Logger.d("===onComplete: ");
            }
        });
    }

    private void t2() {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(@androidx.annotation.NonNull ObservableEmitter<Object> emitter) throws Exception {
                Logger.d("===subscribe: 1");
//                emitter.onNext(1);
                emitter.onError(new Throwable("1"));

            }
        }).flatMap(new Function<Object, ObservableSource<?>>() {
            @Override
            public ObservableSource<?> apply(@androidx.annotation.NonNull Object o) throws Exception {
                Logger.d("===subscribe: 11");

                return Observable.create(new ObservableOnSubscribe<Object>() {
                    @Override
                    public void subscribe(@androidx.annotation.NonNull ObservableEmitter<Object> emitter) throws Exception {
                        Logger.d("===subscribe: 2");
                        emitter.onError(new Throwable("2"));
                    }
                });
            }
        }).flatMap(new Function<Object, ObservableSource<?>>() {
            @Override
            public ObservableSource<?> apply(@androidx.annotation.NonNull Object o) throws Exception {
                Logger.d("===subscribe: 3");

                return Observable.create(new ObservableOnSubscribe<Object>() {
                    @Override
                    public void subscribe(@androidx.annotation.NonNull ObservableEmitter<Object> emitter) throws Exception {
                        Logger.d("===subscribe: 3");
                        emitter.onError(new Throwable("3"));
                    }
                });
            }
        }).retry(2).subscribe(new Observer<Object>() {
            @Override
            public void onSubscribe(@androidx.annotation.NonNull Disposable d) {
                
            }

            @Override
            public void onNext(@androidx.annotation.NonNull Object o) {

            }

            @Override
            public void onError(@androidx.annotation.NonNull Throwable e) {
                Logger.e("===onError: " + e.getMessage());

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void one1() {
        try {
            Observable.just(1)
                    .map(new Function<Integer, Object>() {
                        @Override
                        public Object apply(@NonNull Integer integer) throws Exception {
                            return new RuntimeException();
                        }
                    })
                    .onErrorReturn(new Function<Throwable, Object>() {
                        @Override
                        public Object apply(@NonNull Throwable throwable) throws Exception {
                            Logger.d("RxjavaErrorActivity===apply: " + throwable.getMessage());

                            return throwable.getMessage();
                        }
                    })
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            Logger.d("RxjavaErrorActivity===accept: ");
//                            int a = 100 / 0;
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            Logger.e("RxjavaErrorActivity===: " + throwable.getMessage());

                        }
                    });

        } catch (Exception e) {
            Logger.e("RxjavaErrorActivity===: 1" + e.getMessage());

        }

    }
}
